package cn.virens.packet.handler;

import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;

import cn.virens.App;
import cn.virens.TioUtil;
import cn.virens.app.common.SQL;
import cn.virens.common.ByteUtil;
import cn.virens.common.StringUtil;
import cn.virens.packet.ReadPacket;

public class ReplyHandler extends AbstractHandler {
	private static final Logger logger = LoggerFactory.getLogger(ReplyHandler.class);

	@Override
	public void read(ReadPacket packet, ChannelContext channelContext) {
		logger.debug("接收设备设置回复包！");
		TioUtil.heartTimes(channelContext, (t) -> t.incrementAndGet());
		TioUtil.heartIdle(channelContext, (t) -> t.getAndSet(0));

		Integer type = ByteUtil.toInt(packet.data(0, 1));
		if (type == 1) {
			App.db().execute((s) -> {
				ResultSet cDate = s.executeQuery(SQL.INSTRUCTION_SELECT, packet.seq(), TioUtil.imei(channelContext));
				while (cDate.next()) {
					//取出指令，根据长度来解析对应的指令
					String data = cDate.getString("data");
					String mac = cDate.getString("mac");
					if (StringUtil.isNotEmpty(data)) {
						if (data.length() == 6) {
							//设备开关指令解析:07关机，08开机
							if (StringUtil.equals(data.substring(4, data.length()), "07")) {
								App.db().executeUpdate(SQL.ONLINE_STATUS_UP, 0, cDate.getString("mac"));
								logger.debug("设备状态已改为关闭");
							} else if (StringUtil.equals(data.substring(4, data.length()), "08")) {
								App.db().executeUpdate(SQL.ONLINE_STATUS_UP, 1, cDate.getString("mac"));
								logger.debug("设备状态已改为开启");
							}
						} else if (data.length() == 14) {
							//剩余天数设置
						} else if (data.length() == 74) {
							//激活指令，设置流量
							if (!StringUtil.equals(data.substring(6, 10), "ffff") && StringUtil.equals(data.substring(10, 14), "ffff")) {
								App.db().executeUpdate(SQL.DEVICE_SURPLUS_FLOW_UP, ByteUtil.toInt(data.substring(6, 10)), mac);
							}
							//激活指令，设置天数
							if (StringUtil.equals(data.substring(6, 10), "ffff") && !StringUtil.equals(data.substring(10, 14), "ffff")) {
								App.db().executeUpdate(SQL.DEVICE_SURPLUS_DAYS_UP, ByteUtil.toInt(data.substring(10, 14)), mac);
							}
							//设置设备全部数据
							if (!StringUtil.equals(data.substring(6, 10), "ffff") && !StringUtil.equals(data.substring(10, 14), "ffff")) {
								App.db().executeUpdate(SQL.DEVICE_SURPLUS_FLOW_UP, ByteUtil.toInt(data.substring(6, 10)), mac);
								App.db().executeUpdate(SQL.DEVICE_SURPLUS_DAYS_UP, ByteUtil.toInt(data.substring(10, 14)), mac);
								App.db().executeUpdate(SQL.DEVICE_SURPLUS_DAYS_UP, ByteUtil.toInt(data.substring(50, 58)), "", mac);//sql语句没写
								//TODO 设置全部数据
							}
							//设备滤芯复位
							if (StringUtil.equals(data.substring(6, 10), "ffff") && StringUtil.equals(data.substring(10, 14), "ffff")) {
								logger.debug("设备-->{}，的滤芯已复位", mac);
							}
						}
					}
				}
				return 1;
			});
		}
	}

	public String expireDate(Integer days) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, days);
		return format.format(calendar.getTime());
	}
}
